import { InjectionKey } from 'vue-demi'
import { createStore, ModuleTree, Store, useStore as userBaseStore } from 'vuex'
import getters from './getters'
import { RootState } from './interface'
// 自动导入modules/ 模块
const customModules: ModuleTree<RootState> = {}
const customModuleFiles = import.meta.globEager('./modules/*.ts')

Object.keys(customModuleFiles).forEach((key: string) => {
  const module = customModuleFiles[key].default
  const moduleName: string = key.replace(/^\.\/(.*)\/(.*)\.\w+$/, '$2')
  customModules[moduleName] = module
})

// 定义注入类型
export const key: InjectionKey<Store<RootState>> = Symbol()

const store = createStore<RootState>({
  getters,
  modules: {
    ...customModules
  }
})

export function userStore() {
  return userBaseStore(key)
}

export default store
